/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.tukano.odt;

import it.tukano.fun.Tuple2;
import it.tukano.lang.StringExt;
import java.util.Properties;
import javax.swing.JCheckBox;

/**
 *
 * @author utente4
 */
public class OdtOptionalStringEditor extends OdtFieldEditor {
    
    //Default serial version code
    private static final long serialVersionUID = 0;
    
    private final JCheckBox box = new JCheckBox();
    private final StringExt defaultValue;
    private Boolean mkpar;

    public OdtOptionalStringEditor(Integer size, String label, StringExt defaultValue) {
        this.defaultValue = defaultValue;
        box.setText(label);
        add(box);
    }

    @Override
    public void setValue(StringExt t) {
        box.setSelected(t.toBoolean(false));
    }

    @Override
    public Tuple2<StringExt, String> applyEditorValue(OdtField odtField, String xml) {
        StringExt s = box.isSelected() ? defaultValue : StringExt.wrap("");
        return Tuple2.newInstance(s, xml);
    }

    @Override
    public void clearValue() {
        box.setSelected(false);
    }

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        this.mkpar = new StringExt(properties.getProperty("mkpar", "false")).toBoolean(false);
    }

    public boolean generateOdtParagraph() {
        return this.mkpar;
    }

    public boolean isSelected() {
        return box.isSelected();
    }

    @Override
    public String getStorableStringValue() {
        return String.valueOf(box.isSelected());
    }
}
